home *** CD-ROM | disk | FTP | other *** search
- Path: news.mel.aone.net.au!usenet
- From: clyde@hitech.com.au (Clyde Smith-Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: Examples on hashing - help
- Date: Tue, 23 Jan 1996 12:00:26 GMT
- Organization: HI-TECH Software
- Message-ID: <3104cd53.194778240@news.bne.aone.net.au>
- References: <1996Jan22.164501@omega.ntu.ac.sg>
- Reply-To: clyde@hitech.com.au
- NNTP-Posting-Host: skyhawk.hitech.com.au
- X-Newsreader: Forte Agent .99c/16.141
-
- On 22 Jan 96 16:45:01 +0800, sz7210563@omega.ntu.ac.sg wrote:
-
- >I am currently working on a project that requires me to implement chained
- >hashing. Being new to hashing, I would like to ask if anybody has any C
- >examples of the implementation of chained hash tables, including the basic
- >functions such as insert, delete, search, traverse etc
-
- Here's an example - stripped down from a real application. Need to
- define struct sym, and choose a value for SYM_TAB (size of hash
- table). It should be a prime number.
-
-
- struct sym * sym_tab[SYM_TAB];
-
- short
- hashit(char *s, unsigned hash)
- {
- register unsigned i;
-
- i = 0;
- while(*s)
- i += i + (unsigned char)*s++;
- return(i % hash);
- }
-
- struct sym *
- lkupsy(char * s)
- {
- register struct sym * sy, ** pp;
-
- pp = &sym_tab[hashit(s, SYM_TAB)];
- sy = *pp;
- while(sy && strcmp(sy->s_name, s) != 0))
- sy = sy->s_next;
- if(sy)
- return sy;
- sy = (struct sym *)xalloc(sizeof *sy);
- sy->s_next = *pp;
- *pp = sy;
- sy->s_name = xalloc((unsigned)sylen+1);
- strcpy(sy->s_name, s);
- return(sy);
- }
-
-
- struct sym *
- remsym(struct sym * pp)
- {
- register struct sym ** xp, * qp;;
-
- xp = &sym_tab[hashit(pp->s_name, SYM_TAB)];
- if(*xp == pp) {
- *xp = pp->s_next;
- return pp;
- }
- qp = *xp;
- while(qp->s_next && qp->s_next != pp)
- qp = qp->s_next;
- if(!qp)
- cerror("REMSYM error");
- qp->s_next = pp->s_next;
- return pp;
- }
-
-
- ----
- Clyde Smith-Stubbs | HI-TECH Software, | Voice: +61 7 3300 5011
- clyde@hitech.com.au | P.O. Box 103, Alderley, | Fax: +61 7 3300 5246
- http://www.hitech.com.au | QLD, 4051, AUSTRALIA. | BBS: +61 7 3300 5235
- ----------------------------------------------------------------------------
- FREE! Download our shareware (FREE for noncommercial use) MS-DOS C Compiler!
- Point your Web browser at http://www.hitech.com.au/
-